var requiredCaseStep = new Array(),noRequiredCaseStep =new Array();;//必要环节，非必要环节数组
var ownerQuery = "";
var ownerPageQuery = "";
var ownerPageNewQuery = "";
var vehicleQuery ="";
var vehiclePageQuery="";
var vehiclePageNewQuery="";
var partyQuery= "";
var partyPageQuery="";
var partyPageNewQuery="";
var shipPartyPageNewQuery="";
var shipPageNewQuery="";
var shipPartyQuery="";
var shipQuery="";
/**
 *机构环节文书关联js 
 **/

/**
 * 添加机构环节文书关联数据
 **/
function saveCaseLinkDocument(data){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_LINK_DOCUMENT (LINK_DOCUMENT_ID,DOCNAME,LINKNAME,DOCNAMEID,LINKNAMEID,DOCPATH,SORT,OID,REQUIRED,GXHDOCPATH,TSINPUT) VALUES (?,?,?,?,?,?,?,?,?,?,?)",
    			[data.linkDocumentId,data.docName,data.linkName,data.docNameId,data.linkNameId,data.docPath,data.sort,data.organId,data.required,data.gxhDocPath,data.tsInput],
       function (tx, rs) {
    		
       },
	   function (tx, error) {
    	   message("添加机构环节文书关联数据失败");
	   });
    });
}

/**
 * 通过机构ID获取环节
 **/
function findLinkNameByOid(oid,required,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT DISTINCT LD.LINKNAME,LD.LINKNAMEID,D.SORT,LD.REQUIRED FROM CASE_LINK_DOCUMENT LD,DRAWER D WHERE LD.LINKNAMEID=D.ID AND OID = ? AND LD.REQUIRED = ? ORDER BY D.SORT ASC", [oid,required], function (tx, results){ 
		    var len = results.rows.length; 
		    if(len == 0){
		    	return;
		    }
		    var data = new Array();
		    for (var i = 0; i < len; i++){
		    	var caseLink = {};
		    	caseLink.linkName = results.rows.item(i).LINKNAME;
		    	caseLink.linkNameId = results.rows.item(i).LINKNAMEID;
		    	caseLink.sort = i;
		    	caseLink.required = results.rows.item(i).REQUIRED;;
	    		data.push(caseLink);
		    }
/*			if(requiredCaseStep != undefined && required=='1'){
				requiredCaseStep = data;
			}
			if(noRequiredCaseStep != undefined && required=='0'){
				noRequiredCaseStep = data;
			}*/

			if (typeof(callback) == 'function'){
				callback(data);
			}
			return data;
	    }, 
	    function(tx, ex) {
	    	message("获取与机构关联的环节失败");
        });
	});
}

/**
 * 查询表中数据
 **/
function findLinkDocument(callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT COUNT(*) TOTAL FROM CASE_LINK_DOCUMENT", [], function (tx, results){ 
			var len = results.rows.length;
			var count = 0;
		    for (var i = 0; i < len; i++){
		    	count = results.rows.item(i).TOTAL;
		    }
		    
			if(count){
				/**
				 * 取消定时执行
				 */
				clearInterval(id); 
				$.messager.progress('close');
			}
		    
			 ownerQuery =  $.cookie("owner");
			 ownerPageQuery = $.cookie("ownerPage");
			 ownerPageNewQuery = $.cookie("ownerPageNew");
			 vehicleQuery = $.cookie("vehicle");
			 vehiclePageQuery=$.cookie("vehiclePage");
			 vehiclePageNewQuery=$.cookie("vehiclePageNew");
		     partyQuery= $.cookie("party");
			 partyPageQuery=$.cookie("partyPage");
			 partyPageNewQuery=$.cookie("partyPageNew");
			 shipPartyPageNewQuery=$.cookie("shipPartyPageNew");
			 shipPartyQuery=$.cookie("shipParty");
			 shipPageNewQuery=$.cookie("shipPageNew");
			 shipQuery=$.cookie("ship");
			 
			if (typeof(callback) == 'function'){
				callback(count);
			}
			
			return count;
	    }, 
	    function(tx, ex) {
	    	message("获取与机构关联的环节失败");
        });
	});
}

/**
 * 通过机构ID获取文书
 **/
function findDocsByOid(oid,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT LINKNAMEID,LINKNAME,GXHDOCPATH,TSINPUT,DOCNAMEID,DOCNAME,DOCPATH,SORT FROM CASE_LINK_DOCUMENT WHERE OID = ? AND DOCNAMEID !='1001003700000000' ORDER BY SORT ASC", [oid], function (tx, results){ 
		    var len = results.rows.length; 
		    if(len == 0){
		    	return;
		    }
		    var data = new Array();
		    for (var i = 0; i < len; i++){
		    	var caseLink = {};
		    	caseLink.linkNameId = results.rows.item(i).LINKNAMEID;
		    	caseLink.linkName = results.rows.item(i).LINKNAME;
		    	caseLink.gxhDocPath = results.rows.item(i).GXHDOCPATH;
		    	caseLink.tsInput = results.rows.item(i).TSINPUT;
		    	caseLink.docNameId = results.rows.item(i).DOCNAMEID;
		    	caseLink.docName = results.rows.item(i).DOCNAME;
		    	caseLink.docPath = results.rows.item(i).DOCPATH;
		    	caseLink.sort = results.rows.item(i).SORT;
	    		data.push(caseLink);
		    }
			if (typeof(callback) == 'function'){
				callback(data);
			}
	    }, 
	    function(tx, ex) {
	    	message("获取与机构关联的环节失败");
        });
	});
}

/**
 * 通过机构ID与环节名称获取文书名称、文书路径
 * @param linkNameId 环节名称ID
 * @param oid 机构ID
 **/
function findDocByLinkNameId(linkNameId,oid,callback){
	db.transaction(function (tx) {
		tx.executeSql('SELECT LINKNAME,DOCNAMEID,GXHDOCPATH,TSINPUT,DOCNAME,DOCPATH,SORT FROM CASE_LINK_DOCUMENT WHERE LINKNAMEID = ? AND OID = ? ORDER BY SORT ASC', [linkNameId,oid], 
		function (tx, results){
		    var len = results.rows.length;
		    if(len == 0){
		    	return;
		    }
		    var data = new Array();
		    for (var i = 0; i < len; i++){
		    	var doc = {};
		    	doc.linkNameId = linkNameId;
		    	doc.linkName = results.rows.item(i).LINKNAME;
		    	doc.gxhDocPath = results.rows.item(i).GXHDOCPATH;
		    	doc.tsInput = results.rows.item(i).TSINPUT;
		    	doc.docName = results.rows.item(i).DOCNAME;
		    	doc.docNameId = results.rows.item(i).DOCNAMEID;
		    	doc.docPath = results.rows.item(i).DOCPATH;
		    	doc.sort = results.rows.item(i).SORT;
		    	data.push(doc);
		    }			
		    if (typeof(callback) == 'function'){
				callback(data);
			}
		    return data;
	    }, 
	    function(tx, ex) {
	    	message("获取与机构环节关联的文书名称、文书路径失败");
        });
	});
}

/**
 * 清空数据
 */
function caseLinkDocument(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_LINK_DOCUMENT', [], 
		function (tx, results){
			if (typeof(callback) == 'function'){
				$.each(data,function(index,item){
					callback(item);
				});
			}
		}, 
		function (tx, ex){
			message("删除抽屉数据失败");
		}); 
	});
}

/**
 * 初始化机构环节文书关联数据
 * @param db
 * @param callback
 **/
function initCaseLinkDocument(){
	caseLinkDocument(getCaseLinkDocumentData);
}

//================
/**
 * 从服务器上获取机构环节文书关联所有数据.
 **/
function getCaseLinkDocumentData(){
	$.ajax({
		type : "post",
		url : getContextPath() + "/linkDocument/findAll",
		dataType : "json",
		error : function(request) {
			message("获取机构环节文书关联列表数据失败");
		},
		success : function(data) {
			$.each(data, function(i, item) {
				/**
				 * 添加到本地
				 **/
				saveCaseLinkDocument(item);
			});
			message("获取机构环节文书关联列表数据成功");
		}
	});
}